// import Cookies from 'js-cookie'
import Utils from '../../utils/Common'

const state = {
  sidebar: {
    // opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    opened: true,
    withoutAnimation: false
  },
  device: 'desktop',
  nodeLogger: [], // 节点日志
  flowLogger: [], // 流程日志
  userInfo: null
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      // Cookies.set('sidebarStatus', 1)
    } else {
      // Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    // Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  UPDATE_NODE_LOGGER: (state, data) => {
    if (Utils.IsEmpty(data.id)) {
      return
    }
    let logs = state.nodeLogger[data.id]
    data.Logger.Code = data.Code
    if (Utils.IsEmpty(logs)) {
      logs = [data.Logger]
    } else {
      logs = [data.Logger, ...logs]
    }
    state.nodeLogger[data.id] = logs.slice(0, 100)
    state.flowLogger.push(data.Logger)

    if (state.flowLogger.length > 225) {
      state.flowLogger.shift()
    }
  },
  CLEAR_NODE_LOGGER: (state, NodeId) => {
    delete state.nodeLogger[NodeId]
  },
  SER_USER_INFO: (state, UserInfo) => {
    state.userInfo = UserInfo
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  /**
   * 情况节点日志
   * @param commit
   * @param Node
   */
  clearNodeLogger({ commit }, Node) {
    commit('CLEAR_NODE_LOGGER', Node.id)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
